package md.lanjing.com.module_wallet.ui.activity;

import android.os.Bundle;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.event.BusFactory;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import butterknife.BindView;
import butterknife.OnClick;


import com.lxh.baselibray.constant.Constant;
import com.lxh.baselibray.event.EventImpl;

import md.lanjing.com.module_wallet.R;
import md.lanjing.com.module_wallet.R2;
import md.lanjing.com.module_wallet.bean.ExchangeResponce;
import md.lanjing.com.module_wallet.contact.ExChangeContact;
import retrofit2.Response;

/**
 * 兑换
 */
public class ExChangeActivity extends MvpActivity<ExChangeContact.ExChangePresent> implements ExChangeContact.IExChangeView {
    @BindView(R2.id.et_num)
    EditText etNum;

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_exchange;
    }


    @OnClick(R2.id.tv_confirm_exchange)
    public void onViewClicked() {
        String number = etNum.getText().toString();
        if (ObjectUtils.isEmpty(number)){
            ToastUtils.showLongToast(context,"请输入兑换数量");
            return;
        }
       showDealDialog(Integer.valueOf(number));
    }

    @Override
    public void getExChangeData(Response<ExchangeResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
           ToastUtils.showLongToast(context,"兑换成功");
            BusFactory.getBus().post(new EventImpl.ExchangeEvent());
           finish();
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected ExChangeContact.ExChangePresent createPresent() {
        return new ExChangeContact.ExChangePresent();
    }

    AlertDialog dealDialog = null;
    private void showDealDialog(int number) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd=psw.getPsw();
                    mPresent.getExchange(context,Integer.valueOf(number),dealPwd);
                    dealDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }
}
